"use strict";

/**
 * @ngdoc filter
 * @name jgappApp.filter:NBFmt
 * @function
 * @description
 * # NBFmt
 * Filter in the jgappApp.
 */
angular
  .module("jgappApp")
  .filter("NBFmt", function() {
    return function(input, n) {
      n = n > 0 && n <= 20 ? n : 2;
      if (input) {
        input =
          parseFloat((input + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
        var l = input
            .split(".")[0]
            .split("")
            .reverse(),
          r = input.split(".")[1];
        var t = "";
        for (var i = 0; i < l.length; i++) {
          t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? "," : "");
        }
        return (
          t
            .split("")
            .reverse()
            .join("") +
          "." +
          r
        );
      } else {
        return input;
      }
    };
  })
  .filter("countLimit", function() {
    return function(input) {
      if (input) {
        if (input > 99) {
          return "99+";
        }
        return input;
      }
    };
  })
  .filter("fileSize", function() {
    var formats = {
      Byte: Math.pow(10, 0),
      KB: Math.pow(10, 3),
      MB: Math.pow(10, 6),
      GB: Math.pow(10, 9),
      TB: Math.pow(10, 12),
      PB: Math.pow(10, 15),
      EB: Math.pow(10, 18),
      ZB: Math.pow(10, 21),
      YB: Math.pow(10, 24),
      BB: Math.pow(10, 27)
    };
    function formatFileSize(fileSize, lenght) {
      var r;
      var l = lenght || 2;
      for (var k in formats) {
        var v = formats[k];
        r = fileSize / v;
        if (r >= 1 && r < 1000) {
          r = parseInt(r * Math.pow(10, l), 10) / Math.pow(10, l) + k;
          break;
        }
      }
      return r;
    }
    return function(input) {
      if (input) {
        return formatFileSize(input);
      }
    };
  });
